package com.astrob.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.astrob.NavFrameSDK.POIDescription;
import com.astrob.adapters.CitysAdapter;
import com.astrob.adapters.SearchResultItemAdapter;
import com.astrob.adapters.SearchResultItemAdapter.POIDesc;
import com.astrob.adapters.SearchResultItemAdapter.SeachResultListener;
import com.astrob.model.HistoryData;
import com.astrob.model.KeywordHandle;
import com.astrob.model.LonLat;
import com.astrob.model.Msg;
import com.astrob.model.POITypeObj;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.model.SearchParm;
import com.astrob.model.SpecialTypeBean;
import com.astrob.model.SpecialTypeBean.onQTItemClickListener;
import com.astrob.naviframe.Start;
import com.astrob.view.SpecialTypeView;
import com.astrob.nhcmap.R;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.manager.CityDB;
import zuo.biao.library.model.City;
import zuo.biao.library.ui.PlacePickerWindow;
import zuo.biao.library.util.PlaceUtil;
import zuo.biao.library.util.StringUtil;

public class SearchActivity extends Activity {
	private AutoCompleteTextView mInput;
	private TextView mTvCurCity;
	private int mTypeIndex = 0;
	private double mSearchCenterLon;
	private double mSearchCenterLat;

	SearchResultItemAdapter mAdapter;
	private ListView listView;

	private ListView mKeyListView;
	private CitysAdapter mKeyAdapter = null;

	private Button mBtsearchkey;
	private Button mBtclearkey;
	LonLat mCenterLL;

	private ArrayList<POITypeObj> mPOITypeObjList = new ArrayList<POITypeObj>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_search);
		mSearchCenterLon = Start.getInstance().MapKit().mCityPosition.lon;
		mSearchCenterLat = Start.getInstance().MapKit().mCityPosition.lat;
		mCenterLL = Start.getInstance().MapKit().getMapCenter();
		initUI();
		// onTypeClick(0);
	}

	private void setPOITypeList() {
		mPOITypeObjList.clear();

		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search00,
				"All POI", "All POI", getString(R.string.nearsearch_text_all),
				0xffff7d7d));// 全部
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search09,
				"07. Tourist", "27. Tourist",
				getString(R.string.nearsearch_text_9), 0xff50c7ff));// 景点
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search03,
				"03. 酒店", "23. 酒店", getString(R.string.nearsearch_text_3),
				0xff5ada3d));// 酒店
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search13,
				"11. Railway Station", "31. Railway Station",
				getString(R.string.nearsearch_text_13), 0xff50c7ff));// 地铁

		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search01,
				"01. 餐饮", "21. 餐饮", getString(R.string.nearsearch_text_1),
				0xfffdaa20));// 餐饮
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search04,
				"04. Leisure", "24. Leisure",
				getString(R.string.nearsearch_text_4), 0xfff072ff));// 娱乐
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search02,
				"02. 购物", "22. 购物", getString(R.string.nearsearch_text_2),
				0xff9c70ff));// 购物
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search07,
				"06. Bank", "26. Bank", getString(R.string.nearsearch_text_7),
				0xfffdaa20));// 银行

		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search14,
				"16. Airport", "36. Airport",
				getString(R.string.nearsearch_text_14), 0xff50c7ff));// 机场
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search11,
				"09. Medical", "29. Medical",
				getString(R.string.nearsearch_text_11), 0xfffdaa20));// 药房
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search12,
				"10.	Public Transport Stop", "30.	Public Transport Stop",
				getString(R.string.nearsearch_text_12), 0xff5ada3d));// 公交
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search08,
				"12. Petrol Station", "32. Petrol Station",
				getString(R.string.nearsearch_text_8), 0xff5ada3d));// 加油

		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search16,
				"15. Hospital", "35. Hospital",
				getString(R.string.nearsearch_text_16), 0xff9c70ff));// 医疗
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search10,
				"08. Police Station", "28. Police Station",
				getString(R.string.nearsearch_text_10), 0xffff7d7d));// 警察
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search15,
				"14. Sport", "34. Sport",
				getString(R.string.nearsearch_text_15), 0xfff072ff));// 健身
		mPOITypeObjList.add(new POITypeObj(R.drawable.selector_near_search06,
				"05. Post Office", "25. Post Office",
				getString(R.string.nearsearch_text_6), 0xffff7d7d));// 邮局

	}

	void getSelCity() {
		SharedPreferences sp = getSharedPreferences(
				"CN", Context.MODE_PRIVATE);
		Start.getInstance().MapKit().mSelCityName = sp.getString("selcityname",
				Start.getInstance().MapKit().mSelCityName);
		Start.getInstance().MapKit().mCityPosition.lon = sp.getFloat("citylon",
				(float) Start.getInstance().MapKit().mCityPosition.lon);
		Start.getInstance().MapKit().mCityPosition.lat = sp.getFloat("citylat",
				(float) Start.getInstance().MapKit().mCityPosition.lat);
	}

	SpecialTypeView mgv;
	ArrayList<SpecialTypeBean> mTypeList = null;
	int mPreSelectType = -1; // id must == position

	private void initUI() {
		mTvCurCity = (TextView) findViewById(R.id.title);
		mInput = (AutoCompleteTextView) findViewById(R.id.search_input);
		mgv = (SpecialTypeView) findViewById(R.id.ms_type_around);
		mBtsearchkey = (Button) findViewById(R.id.searchkey_btn);
		mBtsearchkey.setVisibility(View.GONE);
		mBtclearkey = (Button) findViewById(R.id.clearkey_btn);
		mBtclearkey.setVisibility(View.GONE);

		mRadiogroup = (RadioGroup) findViewById(R.id.radiogroup_types);

		mRadiogroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						changeDataType(checkedId);
					}

				});
		mSearchmode1 = (RadioButton) findViewById(R.id.btn_searchmode_poi);
		mSearchmode2 = (RadioButton) findViewById(R.id.btn_searchmode_housenumber);

		mSelectType = 0;
		mSearchmode1.setChecked(true);

		mTvCurCity.setText(Start.getInstance().MapKit().mSelCityName);

		if ("CN".compareToIgnoreCase("twn") == 0) {
			findViewById(R.id.ctrl_searchmode).setVisibility(View.VISIBLE);
		}

		setPOITypeList();
		mTypeList = new ArrayList<SpecialTypeBean>();
		for (int i = 0; i < mPOITypeObjList.size(); ++i) {
			POITypeObj t = mPOITypeObjList.get(i);
			SpecialTypeBean qtb = new SpecialTypeBean(i, t.resTypeIcon,
					t.strTypeName, t.selectedClr, onQTClick);
			mTypeList.add(qtb);
		}
		mgv.setData(mTypeList);

		mAdapter = new SearchResultItemAdapter(this);
		listView = (ListView) findViewById(R.id.listview);
		listView.setVerticalScrollBarEnabled(false);
		listView.setAdapter(mAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				onListItem(arg2);
			}
		});
		mAdapter.setListener(new SeachResultListener() {
			
			@Override
			public void onRoute(POIDesc poi) {
				HistoryData data = new HistoryData();
				data.name = poi.name;
				data.lon = poi.pos.GetLon();
				data.lat = poi.pos.GetLat();
				data.address = poi.addr;

				RoutePlanDataHandle.getInstance().addDest(data);
				RoutePlanActivity.launch(SearchActivity.this, 3);
			}
			
			@Override
			public void onMap(POIDesc poi) {
				m.hideSoftInputFromWindow(mInput.getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				Intent intent = new Intent(SearchActivity.this, NaviActivity.class);
				intent.putExtra("mapcenterlon", poi.pos.GetLon()); //
				intent.putExtra("mapcenterlat", poi.pos.GetLat());
				String poiname = poi.name;
				// 如果是门牌
				if (poiname.contains("路") || poiname.contains("街")) {
					poiname = poi.owner + poiname;
				}
				intent.putExtra("poiname", poiname);
				intent.putExtra("showOnmap", true);
				startActivityForResult(intent, Msg.REQUEST_SHOW_ON_MAP);
			}
			
			@Override
			public void onAround(POIDesc poi) {
				Start.getInstance().MapKit().setMapCenter(new LonLat(poi.pos.GetLon(), poi.pos.GetLat()));
				setResult(Msg.MAP_SEARCHARROUNT);
				finish();
			}
		});
		// mInput.setCursorVisible(false);
		mInput.setThreshold(1000);
		mInput.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
										  KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_NEXT
						|| actionId == EditorInfo.IME_ACTION_DONE
						|| actionId == EditorInfo.IME_ACTION_GO
						|| actionId == EditorInfo.IME_ACTION_SEARCH) {
					onKeyWordChange();
				}
				return false;
			}
		});
		mInput.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				onKeyWordChange();
			}
		});

		final View activityRootView = findViewById(R.id.search_layout);
		activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					private int preHeight = 0;

					@Override
					public void onGlobalLayout() {
						int h1 = activityRootView.getRootView().getHeight();
						int h2 = activityRootView.getHeight();
						int heightDiff = h1 - h2;
						// 在数据相同时，减少发送重复消息。因为实际上在输入法出现时会多次调用这个onGlobalLayout方法。
						if (preHeight == heightDiff) {
							return;
						}
						preHeight = heightDiff;
						if (heightDiff > 100) {
							setKeyboardUI(true);
						}
					}
				});

		mKeyListView = (ListView) findViewById(R.id.keyword_list);
		mKeyAdapter = new CitysAdapter(this);
		mKeyListView.setAdapter(mKeyAdapter);
		mKeyListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				onKeyListItem(arg2);
			}
		});

		setKeyboardUI(true);
		
		mInput.setText("");
		mInput.requestFocus();
		m = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		
	}

	protected void onKeyListItem(int index) {
		String name = KeywordHandle.getInstance().mListKey.get(index);
		mInput.setText(name);
		onKeyWordChange();
	}

	protected void setKeyboardUI(boolean isShow) {
		if (isShow) {
			// 键盘弹出时
			mBtsearchkey.setVisibility(View.VISIBLE);
			mBtclearkey.setVisibility(View.VISIBLE);

			findViewById(R.id.ctrl_keywordlist).setVisibility(View.VISIBLE);
			findViewById(R.id.ctrl_searchmode).setVisibility(View.GONE);
			findViewById(R.id.ctrl_title).setVisibility(View.GONE);
//			findViewById(R.id.view_commend_feature_types).setVisibility(
//					View.GONE);
			findViewById(R.id.listview).setVisibility(View.GONE);
			
			mKeyAdapter.setNameDatas(KeywordHandle.getInstance().mListKey);
			mKeyAdapter.notifyDataSetChanged();	
			mKeyListView.setSelection(0);

		} else {
			mBtsearchkey.setVisibility(View.GONE);
			mBtclearkey.setVisibility(View.GONE);
	
			findViewById(R.id.ctrl_title).setVisibility(View.VISIBLE);
			if ("CN"
					.compareToIgnoreCase("twn") == 0) {
				findViewById(R.id.ctrl_searchmode).setVisibility(View.VISIBLE);
			}
			if (mSelectType == 0) {
				findViewById(R.id.view_commend_feature_types).setVisibility(
						View.VISIBLE);
			}
		
			findViewById(R.id.listview).setVisibility(View.VISIBLE);
			findViewById(R.id.ctrl_keywordlist).setVisibility(View.GONE);
		}

	}

	private RadioGroup mRadiogroup;
	private int mSelectType = 0; // 0: poi 1：门牌
	private RadioButton mSearchmode1;
	@SuppressWarnings("unused")
	private RadioButton mSearchmode2;
	private InputMethodManager m;

	private void changeDataType(int checkedId) {
		int nType = mSelectType;
		if (checkedId == R.id.btn_searchmode_poi) {
			mSelectType = 0;
			findViewById(R.id.view_commend_feature_types).setVisibility(
					View.VISIBLE);
			findViewById(R.id.null_divide).setVisibility(View.GONE);

		} else if (checkedId == R.id.btn_searchmode_housenumber) {
			mSelectType = 1;
			findViewById(R.id.view_commend_feature_types).setVisibility(
					View.GONE);
			findViewById(R.id.null_divide).setVisibility(View.VISIBLE);
		}

		if (nType != mSelectType) {
			m.hideSoftInputFromWindow(mInput.getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
			startQuery();
					
//			mInput.setText("");
//			mAdapter.clear();
//			mAdapter.notifyDataSetChanged();
		}
	}

	protected void onListItem(int pos) {
		m.hideSoftInputFromWindow(mInput.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);

		if (pos < 0 || pos >= Start.getInstance().MapKit().mPOIResult.size())
			return;

		POIDescription poi = Start.getInstance().MapKit().mPOIResult.get(pos);

		double lon = Start.getInstance().MapKit().mPOIResult.get(pos).position.lon;
		double lat = Start.getInstance().MapKit().mPOIResult.get(pos).position.lat;

		String name = Start.getInstance().MapKit().mPOIResult.get(pos).name;
		if (name.contains("\n")) {
			String a[] = name.split("\n");
			name = a[0];
		}

		Start.getInstance().MapKit().mTipPoiList = Start.getInstance().MapKit().getNearPoi(
				new LonLat(lon, lat), false);
		Intent intent = new Intent(this, PoiInfoActivity.class);
		intent.putExtra("tipname", poi.name);
		startActivityForResult(intent, Msg.TO_TIPDETAIL);
	}

	protected void onKeyWordChange() {
		startQuery();
	}

	onQTItemClickListener onQTClick = new onQTItemClickListener() {
		@Override
		public void onClick(View v, int id) {
			onTypeClick(id);
		}
	};

	private void onTypeClick(int id) {
		if (mPreSelectType >= 0 && mPreSelectType < mTypeList.size()) {
			mTypeList.get(mPreSelectType).setSelected(false);
		}
		mTypeList.get(id).setSelected(true);
		mPreSelectType = id;
		mgv.notifyDataChanged();
		// Toast.makeText(this, "U has clicked "+id, Toast.LENGTH_SHORT).show();
		// mInput.setText("");
		mTypeIndex = id;
		startQuery();
	}

	public void onSearch(View v) {
		onKeyWordChange();
	}

	public void onClearkey(View v) {
		mInput.setText("");
	}

	@Override
	public void finish() {
		KeywordHandle.getInstance().save();
		super.finish();
	}

	public void onBack(View v) {
		setResult(Msg.BACK_MAP_ID);
		finish();
	}

	ProgressDialog mPB;
	boolean mIsSearching = false;
	boolean mIsTWNMengzhi = false;

	private void startQuery() {
		if (mIsSearching) {
			return;
		}
		mIsSearching = true;
		m.hideSoftInputFromWindow(mInput.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);

		SearchParm sp = new SearchParm();
		sp.dLon = mSearchCenterLon;
		sp.dLat = mSearchCenterLat;
		POITypeObj t = mPOITypeObjList.get(mTypeIndex);
		sp.poiType = t.strType1;

		if (mSelectType == 1) {
			sp.poiType = "42. Address";
		}

		mIsTWNMengzhi = false;
		if (sp.poiType.compareToIgnoreCase("42. Address") == 0
				&& "CN"
						.compareToIgnoreCase("twn") == 0) {
			mIsTWNMengzhi = true;
		}

		sp.range = 50000;

		sp.keyword = mInput.getText().toString();
		if (sp.keyword.length() < 1) {
			sp.range = 3000;
		} else {
			KeywordHandle.getInstance().add(sp.keyword);
		}

		if (mTypeIndex == 8) {
			// 机场范围为100km
			sp.range = 100000;
		}
		
		if (mSelectType == 1) {
			sp.range = 50000;
		}

		//避免关键字过长
		if (sp.keyword.length() > 20) {
			sp.keyword = sp.keyword.substring(1, 19);
		}
		
		Start.getInstance().MapKit().mSearchParm = sp;
		if (mPB != null) {
			mPB.cancel();
			mPB = null;
		}
		mPB = ProgressDialog.show(this, "正在查询", "查询中...");
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(250);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Start.getInstance().MapKit().nearQuery(false);
				if (mIsTWNMengzhi
						&& Start.getInstance().MapKit().mSearchParm.keyword.length() > 1
						&& Start.getInstance().MapKit().mPOIResult.size() < 1) {
					int keyn = Start.getInstance().MapKit().mSearchParm.keyword
							.indexOf("路");
					if (keyn < 1) {
						keyn = Start.getInstance().MapKit().mSearchParm.keyword
								.indexOf("街");
					}
					if (keyn > 0) {
						mHandler.sendEmptyMessage(Msg.SEARCHNEXT);
						Start.getInstance().MapKit().mSearchParm.keyword = Start
								.getInstance().MapKit().mSearchParm.keyword.substring(0,
								keyn + 1);
						Start.getInstance().MapKit().nearQuery(false);
					}
				}

				mHandler.sendEmptyMessage(Msg.SEARCHFINISH);
			}
		}).start();
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == Msg.SEARCHFINISH) {
				finishedSearch();
			} else if (msg.what == Msg.SEARCHNEXT) {
				Toast.makeText(SearchActivity.this, "此门牌号查询无结果，将继续搜索近似门牌！",
						Toast.LENGTH_LONG).show();
			}
		}

	};

	protected void finishedSearch() {
		if (mPB != null) {
			mPB.dismiss();
			mPB = null;
		}

		List<POIDesc> poiList = new ArrayList<POIDesc>();
		
		for (int i = 0; i < Start.getInstance().MapKit().mPOIResult.size(); i++) {
			POIDesc item = new POIDesc();
			POIDescription poidata = Start.getInstance().MapKit().mPOIResult.get(i);
			item.name = poidata.name;
			item.owner = poidata.owner;
			LonLat poill = new LonLat(poidata.position.lon,
					poidata.position.lat);
			item.pos = poill;
			item.direct = Start.getInstance().MapKit().getDirection(mCenterLL, poill);
			int distance = (int) Start.getInstance().MapKit().getDistance(mCenterLL, poill);
			item.distance = Start.getInstance().MapKit().getDistanceString(distance);
			item.index = i;
			if (poidata.address != null) {
				item.addr = poidata.address;
			}
			if (item.name.contains("路") || item.name.contains("街")) {
				item.addr = poidata.owner;
			} else {
				item.addr = "";
			}
			if (poidata.tel != null) {
				item.phone = poidata.tel;
			}

			// ---------------

			poiList.add(item);
		}

		mAdapter.setDataList(poiList);
		mAdapter.notifyDataSetChanged();

		if (Start.getInstance().MapKit().mPOIResult.size() <= 0) {
			Toast.makeText(this, "查询无结果", Toast.LENGTH_SHORT).show();
		}
		listView.setSelection(0);
		
		setKeyboardUI(false);
		mIsSearching = false;
	}

	private static final int REQUEST_TO_PLACE_PICKER = 10;
	public void onChangeCity(View v) {
		Log.d("THZ", "click city change");
//		Intent intent = new Intent(this, CitySelectActivity.class);
//		intent.putExtra("curCity", Start.getInstance().MapKit().mSelCityName);
//		startActivityForResult(intent, 0);

		//过期后不能查询城市
		long currentMilliseconds = System.currentTimeMillis();
		//2017-10-01
		if (currentMilliseconds > 1506787200000l) {
			Toast.makeText(this, "已过使用期限", Toast.LENGTH_SHORT).show();
			return;
		}


		Intent intent = PlacePickerWindow.createIntent(this, this.getPackageName(), 2);
		startActivityForResult(intent, REQUEST_TO_PLACE_PICKER);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			setResult(Msg.BACK_MAP_ID);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	CityDB cityDB;
	City _selCity;
	public static final String INTENT_PACKAGE_NAME = "INTENT_PACKAGE_NAME";

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Msg.CITY_CHANGED) {
			// 重新查询结果
			mTvCurCity.setText(Start.getInstance().MapKit().mSelCityName);
			mSearchCenterLon = Start.getInstance().MapKit().mCityPosition.lon;
			mSearchCenterLat = Start.getInstance().MapKit().mCityPosition.lat;
			startQuery();
			return;
		}

		if (requestCode == REQUEST_TO_PLACE_PICKER && data != null) {
			ArrayList<String> placeList = data.getStringArrayListExtra(PlacePickerWindow.RESULT_PLACE_LIST);
			if (placeList != null && placeList.size() > PlaceUtil.LEVEL_CITY) {
				String cityName = StringUtil.getTrimedString(placeList.get(PlaceUtil.LEVEL_CITY));
				Start.getInstance().MapKit().mSelCityName = cityName;

				if (cityDB == null) {
					cityDB = CityDB.getInstance(this, StringUtil.getTrimedString(
							getIntent().getStringExtra(INTENT_PACKAGE_NAME)));
				}
				_selCity = cityDB.getCity(cityName);
				if (_selCity != null) {
					Start.getInstance().MapKit().mCityPosition.lon = _selCity.getLongitude();
					Start.getInstance().MapKit().mCityPosition.lat = _selCity.getLatitude();

					// 重新查询结果
					mTvCurCity.setText(Start.getInstance().MapKit().mSelCityName);
					mSearchCenterLon = Start.getInstance().MapKit().mCityPosition.lon;
					mSearchCenterLat = Start.getInstance().MapKit().mCityPosition.lat;
					startQuery();
				}

			}

			return;
		}

		if (resultCode == Msg.NAVI_TO_DEST) {
			setResult(Msg.NAVI_TO_DEST);
			finish();
			return;
		}

		if (resultCode == Msg.MAP_SEARCHARROUNT) {
			// onTypeClick(0);
			setResult(Msg.MAP_SEARCHARROUNT);
			finish();
			return;
		}

		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
}
